/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog ,datepicker) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'home_banner',
                condition: {type: '1'},
                model: {
                    url:_global.url.api + 'home_banner',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                        '.show-offline:click': 'sethidden',
                        '.show-online:click': 'setshow',
                        '.edit_listorder:change': 'setlistorder'
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'修改首页广告',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function() {
                                _this.data.show = $("input[name='show']:checked").val();
                                _this.data.img = $("input[name='imgs']").val();
                                if(_this.data.img==''||_this.data.img==undefined){
                                    Alert({msg:'请传入一张图片',type:'error'});
                                    return false;
                                }
                                _this.data.title = $('#title').val();
                                if(_this.data.title==''){
                                    Alert({msg:'请传入标题',type:'error'});
                                    return false;
                                }
                                _this.data.url = $('#url').val();
                                if(_this.data.url==''){
                                    Alert({msg:'请传入链接',type:'error'});
                                    return false;
                                }
                                _this.data.start_time = $('#dt_start').val();
                                _this.data.end_time = $('#dt_end').val();
                                if(_this.data.start_time!=''||_this.data.end_time!=''){
                                    if(_this.data.start_time==''||_this.data.end_time==''){
                                        Alert({msg: '开始时间或者结束时间不能为空',type: 'error'});
                                        return false;
                                    }
                                    var date1 = new Date(Date.parse( _this.data.start_time));
                                    var date2 = new Date(Date.parse(_this.data.end_time));
                                    if (date1.getTime() > date2.getTime()) {
                                        Alert({msg: '开始时间不能大于结束时间',type: 'error'});
                                        return false;
                                    }
                                }
                                _this.data.desc = $('#desc').val();
                                _this.data.listorder = $('#listorder').val();
                                _this.data.type = 1;
                                _this.data.id = _this.data.id;
                               if(model.put()){
                                   _this.collection.reload();
                               };
                            },cancel: function() {}
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function() {
                            WdatePicker({
                                el:this.id,
                                dateFmt:'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setshow:function(e){
                        e.preventDefault();
                        var id =this.data.id;
                        var model =this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                if(new Model({
                                        url: _global.url.api + 'home_banner_set',
                                        data:{id:id,is_visible:1,act:'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    sethidden:function(e){
                        e.preventDefault();
                        var id =this.data.id;
                        var model =this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                if(new Model({
                                        url: _global.url.api + 'home_banner_set',
                                        data:{id:id,is_visible:0,act:'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setlistorder:function(e){
                        e.preventDefault();
                        var id = this.data.id;
                        var model =this;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if(new Model({
                                url: _global.url.api + 'home_banner_set',
                                data:{id:id,listorder:listorder,act:'order'}
                            }).put()) {
                            _this.collection.fetch();
                        };
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加首页广告',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function() {
                        _this.data.show = $("input[name='show']:checked").val();
                        _this.data.img = $("input[name='imgs']").val();
                        if(_this.data.img==''||_this.data.img==undefined){
                            Alert({msg:'请传入一张图片',type:'error'});
                            return false;
                        }
                        _this.data.title = $('#title').val();
                        if(_this.data.title==''){
                            Alert({msg:'请传入标题',type:'error'});
                            return false;
                        }
                        _this.data.url = $('#url').val();
                        if(_this.data.url==''){
                            Alert({msg:'请传入链接',type:'error'});
                            return false;
                        }
                        _this.data.start_time = $('#dt_start').val();
                        _this.data.end_time = $('#dt_end').val();
                        if(_this.data.start_time!=''||_this.data.end_time!=''){
                            if(_this.data.start_time==''||_this.data.end_time==''){
                                Alert({msg: '开始时间或者结束时间不能为空',type: 'error'});
                                return false;
                            }
                            var date1 = new Date(Date.parse( _this.data.start_time));
                            var date2 = new Date(Date.parse(_this.data.end_time));
                            if (date1.getTime() > date2.getTime()) {
                                Alert({msg: '开始时间不能大于结束时间',type: 'error'});
                                return false;
                            }
                        }
                        _this.data.desc = $('#desc').val();
                        _this.data.listorder = $('#listorder').val();
                        _this.data.type = 1;
                        if(new Model({
                            url: _global.url.api + 'home_banner',
                            data: _this.data
                        }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function() {}
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
                $('.long_date').on('click', function() {
                    WdatePicker({
                        el:this.id,
                        dateFmt:'yyyy-MM-dd HH:mm:ss'
                    });
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function(e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function() {
                        console.log(123123123);
                        console.log('contentWindow',$("#upload-frame")[0].contentWindow.image);
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function() {}
                });
                _this.upload_dialog.show();
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function(img) {
            console.log('this.data',this.data);
            if(typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});